package com.wangxinenpu.springbootdemo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wangxinenpu.springbootdemo.dao.mapper.RoleMapper;
import com.wangxinenpu.springbootdemo.dataobject.dto.function.FunctionTreeVo;
import com.wangxinenpu.springbootdemo.dataobject.dto.role.RoleDto;
import com.wangxinenpu.springbootdemo.dataobject.dto.role.RoleFunDto;
import com.wangxinenpu.springbootdemo.dataobject.model.System;
import com.wangxinenpu.springbootdemo.dataobject.model.*;
import com.wangxinenpu.springbootdemo.service.*;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 角色表 服务实现类
 * </p>
 *
 * @author =
 * @since 2024-06-14
 */
@Service
@Setter ( onMethod_ = @Resource )
public class RoleServiceImpl extends ServiceImpl < RoleMapper,Role > implements RoleService {
	private RoleFunctionService roleFunctionService;
	private SystemService sysService;
	private SysOrgService sysOrgService;
	private FunctionService functionService;


	@Override
	public R < Page < Role > > pageList( RoleDto dto ) {
		Page < Role > page = new Page <>( dto.getPageNum() , dto.getPageSize() );
		Page < Role > rolePage = page( page ,
				new LambdaQueryWrapper < Role >().eq( Role::getOrgId , dto.getOrgId() )
						.like( StringUtils.isNotBlank( dto.getRoleCode() ) , Role::getRoleCode , dto.getRoleCode() )
						.like( StringUtils.isNotBlank( dto.getRoleName() ) , Role::getRoleCode , dto.getRoleName() )
						.like( StringUtils.isNotBlank( dto.getRemark() ) , Role::getRoleCode , dto.getRemark() ) );
		return R.ok( rolePage );
	}

	@Override
	@Transactional ( rollbackFor = Exception.class )
	public R < Boolean > delete( List < String > ids ) {
		if ( CollectionUtils.isEmpty( ids ) ) return R.failed( "请选择删除内容" );
		return R.ok( removeByIds( ids ) );
	}

	@Override
	public R < Boolean > saveOrUpdate( RoleDto dto ) {
		Role role = new Role();
		BeanUtils.copyProperties( dto , role );
		return R.ok( saveOrUpdate( role ) );
	}

	@Override
	@Transactional ( rollbackFor = Exception.class )
	public R < Boolean > configurationFunction( RoleFunDto dto ) {
		roleFunctionService.remove( new LambdaQueryWrapper < RoleFunction >().eq( RoleFunction::getRoleId ,
				dto.getId() ) );

		List < RoleFunction > roleFunctions = new ArrayList <>();
		dto.getFunIds().forEach( item -> {
			RoleFunction roleFunction = new RoleFunction();
			roleFunction.setRoleId( dto.getId() );
			roleFunction.setFunId( item );
			roleFunctions.add( roleFunction );
		} );

		return R.ok( roleFunctionService.saveBatch( roleFunctions ) );
	}

	@Override
	public R < List < FunctionTreeVo > > treeFun( String id ) {
		// 查询角色所在组织根据组织查询组织所在的系统和其功能点
		Role role = getById( id );

		List < Function > functionList = functionService.list();

		List < SysOrg > orgList = sysOrgService.list( new LambdaQueryWrapper < SysOrg >().eq( SysOrg::getOrgId ,
				role.getOrgId() ) );
		if ( CollectionUtils.isEmpty( orgList ) ) {
			return R.ok( null );
		}

		List < System > systemList = sysService.list( new LambdaQueryWrapper < System >().in( System::getId ,
				orgList.stream().map( SysOrg::getSysId ).collect( Collectors.toList() ) ) );

		List < FunctionTreeVo > treeVoList = systemList.stream().map( item -> {
			FunctionTreeVo functionTreeVo = new FunctionTreeVo();
			functionTreeVo.setId( item.getId() );
			functionTreeVo.setFunctionCode( item.getSystemCode() );
			functionTreeVo.setFunctionName( item.getSystemName() );
			functionTreeVo.setChildLevel( functionService.getChildLevel( functionTreeVo , functionList ) );
			return functionTreeVo;
		} ).collect( Collectors.toList() );

		return R.ok( treeVoList );
	}


}
